package com.my.house.controller;

import com.my.house.common.ApiAuth;
import com.my.house.common.ApiResult;
import com.my.house.common.Constants;
import com.my.house.common.utils.CopierUtil;
import com.my.house.common.utils.DegisterUtil;
import com.my.house.common.utils.ThreadLocalHolder;
import com.my.house.domain.User;
import com.my.house.service.*;
import com.my.house.vo.*;
import jakarta.servlet.http.HttpSession;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.Objects;

/**
 * <p>
 * 系统用户表 前端控制器
 * </p>
 *
 * @author yangll
 * @since 2025-01-22
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/api/user")
@RequiredArgsConstructor
public class UserController {
    private final UserService userService;
    private final BuildingService buildingService;
    private final RoomService roomService;


    @PostMapping("register")
    @ApiAuth(anonymous = true)
    public ApiResult<Void> register(HttpSession session, @Validated @NotNull @RequestBody RegisterReqVO vo) {
        User mobile = userService.getByMobile(vo.getMobile());
        if (Objects.nonNull(mobile)) {
            return ApiResult.error("手机号已被使用");
        }
        User user = CopierUtil.convertT(vo, User.class);
        assert user != null;
        user.setPassword(DegisterUtil.md5(user.getPassword()));
        userService.save(user);
        session.setAttribute(Constants.USER, user);
        return ApiResult.ok();
    }

    @PostMapping("login")
    @ApiAuth(anonymous = true)
    public ApiResult<Void> login(HttpSession session, @Validated @NotNull @RequestBody LoginReqVO vo) {
        User user = userService.getByMobile(vo.getMobile());
        if (Objects.isNull(user) || !Objects.equals(user.getPassword(), DegisterUtil.md5(vo.getPassword()))) {
            return ApiResult.error("手机号或密码错误");
        }
        session.setAttribute(Constants.USER, user);
        log.info("{}---登录成功--{}",vo.getMobile(), session.getId());
        return ApiResult.ok();
    }

    @PostMapping("logout")
    public ApiResult<Void> logout(HttpSession session) {
        session.invalidate();
        return ApiResult.ok();
    }

    @PostMapping("main")
    public ApiResult<UserMainRespVO> userMain(@RequestBody @Validated DateRangeReqVO reqVO) {
        User user = ThreadLocalHolder.getUser();
        assert user != null;
        return ApiResult.ok(userService.getUserMainInfo(user, reqVO.getStartDate(), reqVO.getEndDate()));
    }

    @PostMapping("resetWatPrice")
    public ApiResult<Void> resetWatPrice(@RequestBody @Validated ResetPriceReqVO reqVO) {
        User user = ThreadLocalHolder.getUser();
        assert user != null;
        if (userService.resetWatPriceByUserId(user.getId(), reqVO.getPrice())) {
            user.setWaterPrice(reqVO.getPrice());
        }
        return ApiResult.ok();
    }

    @PostMapping("resetElePrice")
    public ApiResult<Void> resetElePrice(@RequestBody @Validated ResetPriceReqVO reqVO) {
        User user = ThreadLocalHolder.getUser();
        assert user != null;
        if (userService.resetElePriceByUserId(user.getId(), reqVO.getPrice())) {
            user.setElectricPrice(reqVO.getPrice());
        }
        return ApiResult.ok();
    }

    @PostMapping("resetRentDay")
    public ApiResult<Void> resetRentDay(@RequestBody @Validated ResetRentDayReqVO reqVO) {
        User user = ThreadLocalHolder.getUser();
        assert user != null;
        if (userService.resetRentDayByUserId(user.getId(), reqVO.getDay())) {
            user.setRentDay(reqVO.getDay());
        }
        return ApiResult.ok();
    }
}
